/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.renren.modules.sys.service.impl;


import io.renren.modules.sys.service.MailService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Properties;


/**
 * 系统用户
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年9月18日 上午9:46:09
 */
@Service("mailService")
public class MailServiceImpl implements MailService {
	@Value("${prop.mail.host}")
	private String host;
	@Value("${prop.mail.password}")
	private String password;
	@Value("${prop.mail.protocol}")
	private String protocol;
	@Value("${prop.mail.port}")
	private Integer port;
	@Value("${prop.mail.from}")
	private String from;
	@Resource
	private Properties JavaMailProperties;

	@Override
	public JavaMailSenderImpl newSender() {
		JavaMailSenderImpl sender = new JavaMailSenderImpl();
		sender.setHost(host);
		sender.setPassword(password);
		sender.setPort(port);
		sender.setProtocol(protocol);
		sender.setUsername(from);
		JavaMailProperties.setProperty("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
		JavaMailProperties.setProperty("mail.smtp.socketFactory.port", "465");
		JavaMailProperties.setProperty("mail.smtp.auth", "true");
		JavaMailProperties.setProperty("mail.debug", "false");
		JavaMailProperties.setProperty("mail.smtp.timeout", "25000");
		sender.setJavaMailProperties(JavaMailProperties);
		return sender;
	}
}
